package com.seculab.board.web;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.seculab.board.domain.Board;
import com.seculab.board.domain.Comment;
import com.seculab.board.service.BoardService;
import com.seculab.common.util.CustomPaginationInfo;
import com.seculab.member.domain.Member;

@Controller
@RequestMapping("/board")
public class BoardController {

	@Autowired
	private BoardService boardService;
	
	@Autowired
	private CustomPaginationInfo customPaginationInfo;	
	
	@RequestMapping("boardList")
	public void boardList(Model model, Board board) throws Exception {
		int totalCnt = 0;
		
		board.setCustomPaginationInfo(customPaginationInfo);
		List boardList = boardService.getBoardList(board);
		if(boardList.size() > 0) {
			totalCnt = ((BigDecimal)((Map)boardList.get(0)).get("totalCnt")).intValue();
		}
		
		customPaginationInfo.setTotalRecordCount(totalCnt);
		model.addAttribute("boardList", boardList);
		model.addAttribute("paginationInfo", customPaginationInfo);
	}
	
	@RequestMapping("regiBoard")
	public void regiBoard() {
	}	
	
	@RequestMapping("insertBoard")
	public String insertBoard(HttpServletRequest request, Board board, HttpSession session) throws Exception {
		Member member = (Member)session.getAttribute("member");
		board.setReg_id(member.getId());
		board.setUpd_id(member.getId());
		board.setDiv("01");
		board.setIp_addr(request.getRemoteAddr());
		boardService.insertBoard(board);
		
		return "redirect:/app/board/boardList";
	}	
	
	@RequestMapping("boardView")
	public void boardView(Model model, Board param) throws Exception {
		Map<String, Object> board = boardService.getBoard(param);
		Comment comment = new Comment();
		comment.setBoard_num(param.getNum());
		List commentList = boardService.getCommentList(comment);
		
		model.addAttribute("param", param);
		model.addAttribute("board", board);
		model.addAttribute("commentList", commentList);
	}	
	
	@RequestMapping("updBoard")
	public void updBoard(Model model, Board param) throws Exception {
		Map<String, Object> board = boardService.getBoard(param);
		
		model.addAttribute("param", param);
		model.addAttribute("board", board);		
	}	
	
	@RequestMapping("updateBoard")
	public String updateBoard(HttpServletRequest request, Board board, HttpSession session) throws Exception {
		Member member = (Member)session.getAttribute("member");
		board.setUpd_id(member.getId());
		board.setIp_addr(request.getRemoteAddr());
		boardService.updateBoard(board);
		
		return "redirect:/app/board/boardList";
	}	
	
	@RequestMapping(value="comment", method=RequestMethod.POST)
	public void comment(Model model, Comment param, HttpSession session) throws Exception {
		Member member = (Member)session.getAttribute("member");
		param.setReg_id(member.getId());
		param.setBoard_num(param.getNum());
		
		boardService.insertComment(param);
		Map<String, Object> comment = boardService.getComment(param);
		model.addAttribute("comment", comment);	
	}
	
	@RequestMapping(value="comment", method=RequestMethod.GET)
	public void commentResult(Model model, Comment param) throws Exception {
		int result = boardService.deleteComment(param);
		model.addAttribute("result", result);
	}	

}
